(function () {
    initVue(function () {
        axioisMenuData("node", function (response) {
            window.vue.$data.root.data = response.data.data;
        });
    });

    /**
     * 请求菜单的数据
     * @param parentCode
     * @param callbackFun
     */
    function axioisMenuData(parentCode, callbackFun) {
        console.log(`传递到axios函数里面的code是：${parentCode}`);
        if (typeof (parentCode) == "undefined") {
            parentCode = "node";
        }
        axios.get(`${_config_.server}${_config_.app.menu}?parentCode=${parentCode}`)
            .then(callbackFun)
            .catch(function (error) {
                console.log(error);
            });
    }

    /***
     *菜单项的点击
     * @param key
     * @param keyPath
     */
    function menuItemClick(key, keyPath) {
        console.log(`传递到menuItemClick()key：${key}`);
        var level = key.split("-").length
        switch (level) {
            case 2:
                axioisMenuData(key, function (response) {
                    vue.$data.one.data = response.data.data;
                    vue.$data.two.data = vue.$data.three.data = [];
                });
                break;
            case 3:
                axioisMenuData(key, function (response) {
                    vue.$data.two.data = response.data.data;
                    vue.$data.three.data = [];
                });
                break;
            case 4:
                axioisMenuData(key, function (response) {
                    vue.$data.three.data = response.data.data;
                });
                break;
        }

    }

    /**
     *设置vue的method对象
     * @returns {{handleSelect: (function(*=, *=, *)), addMenu: (function(*)), editMenu: (function(*=)), deleteMenu: (function(*))}}
     */
    function getVueMethods() {
        var methods = {
            handleSelect(key, keyPath, location) {
                location.activeIndex = key;
                menuItemClick(key, keyPath)
            },
            addMenu(parentNode) {
                vue.menuInfo.dialogTitle="菜单新增"
                this.menuInfo.dialogSrc = "edit/menuInfo.html?parentCode=" + parentNode;
                this.dialogVisible = true;
            },
            editMenu(location) {
                vue.menuInfo.dialogTitle="菜单编辑"
                this.menuInfo.dialogSrc = "edit/menuInfo.html?code=" + location;
                this.dialogVisible = true;
            },
            deleteMenu(location) {
                window.vue.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'error'
                }).then(() => {
                    axiosDeleteMenu(this[location].activeIndex);
                }).catch((error) => {
                    console.error(error);
                    window.vue.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            }
        }
        return methods;
    }

    /**
     *获取watch
     * @returns {{dialogVisible: dialogVisible}}
     */
    function getVueWatches() {
        var methods = {
            dialogVisible: function (val, newVal) {
                if (val === true) {
                    vue.$data.menuInfo.iframeStyle.display = "block"
                } else {
                    vue.$data.menuInfo.iframeStyle.display = "none"
                }
            },

        }
        return methods;
    }

    /**
     * 初始化vue数据模型
     * @param callback
     */
    function initVue(callback) {
        var vueData = {
            root: {
                data: [],
                activeIndex: ""
            },
            one: {
                data: [],
                activeIndex: ""
            },
            two: {
                data: [],
                activeIndex: ""
            },
            three: {
                data: [],
                activeIndex: ""
            },
            dialogVisible: false,
            menuInfo: {
                iframeStyle: {
                    width: "100%",
                    height: "410px",
                    display: "none"
                },
                dialogTitle: "",
                dialogSrc: "edit/menuInfo.html"
            }

        }
        var Main = {
            el: "#app",
            data: vueData,
            methods: getVueMethods(),
            watch: getVueWatches()
        }
        window.vue = new Vue(Main);
        callback();
    }

    /**
     *执行删除的远程调用
     * @param code
     */
    function axiosDeleteMenu(code) {
        axios.delete(`${_config_.server}${_config_.app.menu}?code=` + code)
            .then(function (response) {
                window.vue.$message({
                    type: 'success',
                    message: '删除成功!'
                });
            })
            .catch(function (error) {
                window.vue.$message({
                    type: 'error',
                    message: '删除失败!'
                });
            });
    }

})()

